package com.skyline.attraction.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.skyline.attraction.dao.AttractionRefrenceDao;
import com.skyline.attraction.model.Attraction;
import com.skyline.attraction.model.AttractionRefrence;
import com.skyline.attraction.service.AttractionRefrenceService;
import com.skyline.attraction.type.AttractionRefrenceType;
import com.skyline.base.exception.NoOperatePermissionException;
import com.skyline.base.exception.NoResourceException;
import com.skyline.base.type.Activity;
import com.skyline.base.type.Authority;
import com.skyline.common.bean.Page;
import com.skyline.user.model.User;
import com.skyline.wo.model.Album;
import com.skyline.wo.model.Article;

@Service("attractionRefrenceService")
public class AttractionRefrenceServiceImpl implements AttractionRefrenceService {

	@Autowired
	private AttractionRefrenceDao attractionRefrenceDao;

//	@Override
//	public List<AttractionRefrence> getRefrencesByAttractionId(Long attractionId, Page page) {
//		return attractionRefrenceDao.queryRefrencesByAttractionId(attractionId, Activity.NORMAL, page);
//	}

	@Override
	public AttractionRefrence getRefrenceById(Long id) {
		AttractionRefrence refrence = attractionRefrenceDao.queryRefrenceById(id, Activity.NORMAL);
		if (refrence == null) {
			throw new NoResourceException("ID为[" + id + "]的景点引用不存在，可能已被删除");
		}
		return refrence;
	}

	@Override
	public List<AttractionRefrence> listRefrencesOfAttraction(Long attractionId, AttractionRefrenceType type,
			Page page) {
		if (type == null) {
			return attractionRefrenceDao.queryRefrencesByAttractionId(attractionId, Activity.NORMAL, page);
		} else {
			return attractionRefrenceDao.queryRefrencesByAttractionIdAndType(attractionId, type, Activity.NORMAL, page);
		}
	}

	public AttractionRefrence getRefrenceForChange(Long id, AttractionRefrenceType type, Long adminId) {
		AttractionRefrence refrence = getRefrenceForChange(id, adminId);
		if (!refrence.getRefType().equals(type)) {
			throw new NoOperatePermissionException("景点类型有误，无法修改ID为[" + id + "]的景点引用");
		}
		return refrence;
	}

	@Override
	public AttractionRefrence getRefrenceForChange(Long id, Long adminId) {
		AttractionRefrence refrence = getRefrenceById(id);
		if (!refrence.getProviderId().equals(adminId)) {
			throw new NoOperatePermissionException("不是管理员无法修改ID为[" + id + "]的景点引用");
		}
		return refrence;
	}

	@Override
	public Long addRefrence(Long refId, String refPortrait, String refTitle, String refDigest,
			AttractionRefrenceType refType, Attraction attraction, User provider) {
		Long attractionId = attraction.getId();
		String attractionName = attraction.getName();
		String attractionPortrait = attraction.getPortrait();

		Long providerId = provider.getId();
		String providerNickname = provider.getNickname();
		String providerPortrait = provider.getPortrait();
		String providerEmail = provider.getEmail();

		return attractionRefrenceDao.insertRefrence(attractionId, attractionName, attractionPortrait, refId,
				refPortrait, refTitle, refDigest, refType, providerId, providerNickname, providerPortrait,
				providerEmail);
	}

	@Override
	public void deleteRefrenceById(Long id, Long adminId) {
		AttractionRefrence refrence = getRefrenceForChange(id, adminId);
		attractionRefrenceDao.updateRefrenceActivity(refrence.getId(), Activity.NORMAL);
	}

	@Override
	public void updateRefPortrait(Long id, String portrait) {
		attractionRefrenceDao.updateRefPortrait(id, portrait);
	}

	@Override
	public boolean isRefrenceExist(Long attractionId, AttractionRefrenceType type, Long refId) {
		Integer count = attractionRefrenceDao.countTargetRefrenceOfAttraction(attractionId, type, refId,
				Activity.NORMAL);
		return (count != null && count > 1);
	}

	@Override
	public List<Article> queryArticlesWithRefed(Long ownerId, Long attractionId, Page page) {
		return attractionRefrenceDao.queryArticlesWithRefed(ownerId, attractionId, Activity.NORMAL, Authority.PUBLIC,
				page);
	}

	@Override
	public List<Album> queryAlbumsWithRefed(Long ownerId, Long attractionId, Page page) {
		return attractionRefrenceDao.queryAlbumsWithRefed(ownerId, attractionId, Activity.NORMAL, Authority.PUBLIC,
				page);
	}

	public List<AttractionRefrence> listSomeRefrencesOfAttraction(Long attractionId, AttractionRefrenceType type, Integer size) {
		if(type == null) {
			return attractionRefrenceDao.querySomeRefrencesOfAttraction(attractionId, Activity.NORMAL, size);
		} else {
			return attractionRefrenceDao.querySomeRefrencesOfAttractionWithType(attractionId, type, Activity.NORMAL, size);
		}
	}
}
